import prompt from '@system.prompt';

export default {
    props:{
        compData:[],
        compBtndata:[],
        compIsleft:false,//默认menu在右侧
    },
    data: {
        moveX:0,
        moveStartX:0,
        touchMoveX:0,
        btnAllViewWidth:0,//滑动按钮整体宽
        index:-1,
    },

    onInit() {
        for (var i = 0; i < this.compBtndata.length; i++) {
            this.btnAllViewWidth += this.compBtndata[i].width;
        }
        console.info("this.compIsleft："+this.compIsleft)
    },

    moveResTo(){
        for (var i = 0; i < this.compData.length; i++) {
            this.compData[i].itemMoveX = 0;
        }
    },
    touchstart(index,e){
        this.moveResTo();
        this.moveStartX = e.touches[0].globalX;
//        this.index = msg.target.children[0].watchers[0].vm.$idx;//触摸下标此方法同样有效
        this.index = index;

    },
    touchmove(e){
        console.info("touchmove  compIsleft："+this.compIsleft)
        this.touchMoveX = e.touches[0].globalX;

        if(this.compIsleft){
            if(this.touchMoveX > this.moveStartX){//往右滑
                if(this.touchMoveX-this.moveStartX < this.btnAllViewWidth){
                    this.compData[this.index].itemMoveX = this.touchMoveX-this.moveStartX
                }
            }
        }else{
            if(this.moveStartX>this.touchMoveX){//往左滑
                if(this.moveStartX-this.touchMoveX < this.btnAllViewWidth){
                    this.compData[this.index].itemMoveX = -(this.moveStartX-this.touchMoveX);
                }
            }
        }
        console.info("this.moveStartX："+this.moveStartX+" this.touchMoveX："+this.touchMoveX)

    },

    touchend(){
        console.info("touchend  compIsleft："+this.compIsleft)

        if(Math.abs(this.compData[this.index].itemMoveX)<40){
            this.moveResTo();
        }
        if(Math.abs(this.compData[this.index].itemMoveX)>=40){
            if(this.compIsleft){
                this.compData[this.index].itemMoveX = this.btnAllViewWidth;
            }else{
                this.compData[this.index].itemMoveX = -this.btnAllViewWidth;
            }
        }
    },
    onclick(){
        //事件参数
        this.$emit("eventItemclick",{
            itemobj:this.compData[this.index],
            index:this.index
        });//注意事项不能包含2个大写字母，否则失效
    },
    itemClick(item){
        this.$emit("eventItembtnclick",
            {
                btnObj:item,itemObj:this.compData[this.index],
                index:this.index
            })
    },


}
